package com.chilly.io;

import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * Created by Chilly Cui on 2020/9/24.
 */
public class InputStreamReaderDemo {

    @Test
    void test2() throws IOException {
        // 创建流对象,默认UTF8编码
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("c:/env/三国/黄忠.txt"));
        osw.write("你好"); // 保存为6个字节

        // 创建流对象,指定GBK编码
        OutputStreamWriter osw2 = new OutputStreamWriter(new FileOutputStream("c:/env/三国/马超.txt"), "GBK");
        osw2.write("你好");// 保存为4个字节

        osw.flush();
        osw2.flush();
    }

    @Test
    void test() throws IOException {

        // 创建流对象,默认UTF8编码
        InputStreamReader isr = new InputStreamReader(new FileInputStream("c:/env/三国/utf8.txt"), "UTF8");
        // 创建流对象,指定GBK编码
        InputStreamReader isr2 = new InputStreamReader(new FileInputStream("c:/env/三国/utf8.txt"), "GBK");

        int read;
        while ((read = isr.read()) != -1) {
            System.out.println((char) read);
        }

        while ((read = isr2.read()) != -1) {
            System.out.println((char) read);
        }

    }
}
